package com.ldc.framework.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.widget.ViewPager2
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMFragment
import com.ldc.framework.R
import com.ldc.framework.adapter.FindFragmentAdapter
import com.ldc.framework.databinding.FrameworkFragmentFindBinding
import com.ldc.framework.viewmodel.FrameworkViewModelBase

class FindFragment : VBMFragment<FrameworkFragmentFindBinding, FrameworkViewModelBase>() {
    companion object {
        private const val ARG_USER_ID: String = "ARG_USER_ID"
        fun newInstance(userId: String = ""): FindFragment = FindFragment().apply {
            arguments = Bundle().apply {
                putString(ARG_USER_ID, userId)
            }
        }

    }


    private var fragmentAdapter: FindFragmentAdapter? = null

    override fun main() {
        initAdapter()
    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FrameworkFragmentFindBinding =
        FrameworkFragmentFindBinding.inflate(inflater, container, false)

    override fun createViewModel(): FrameworkViewModelBase =
        ViewModelUtils.create(this, FrameworkViewModelBase::class.java)


    private fun initAdapter() {
        fragmentAdapter = FindFragmentAdapter(requireActivity() as FragmentActivity).apply {
            newData(
                mutableListOf(
                    WxFragment.newInstance(),
                    SystemFragment.newInstance(),
                    ProjectsFragment.newInstance(),
                    NavFragment.newInstance()
                )
            )
        }
        viewBind?.also {
            it.vp2.adapter = fragmentAdapter
            it.vp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    changePage(position)
                }
            })
            it.vp2.currentItem = 0
            it.vp2.offscreenPageLimit = 3
            it.flWx.setOnClickListener { changePage(0) }
            it.flSystem.setOnClickListener { changePage(1) }
            it.flProject.setOnClickListener { changePage(2) }
            it.flNav.setOnClickListener { changePage(3) }
        }
    }

    // 设置动态切换
    private fun changePage(pos: Int = 0) {
        viewBind?.let {
            it.vp2.also { viewPager2 ->
                viewPager2.currentItem = pos
            }
            // 公众号
            it.vWx.visibility = if (pos == 0) View.VISIBLE else View.INVISIBLE
            it.tvWx.setTextColor(
                if (0 == pos) resources.getColor(
                    R.color.res_color_ff824c
                ) else
                    resources.getColor(R.color.res_color_font_normal)
            )
            // 体系
            it.vSystem.visibility = if (pos == 1) View.VISIBLE else View.INVISIBLE
            it.tvSystem.setTextColor(
                if (1 == pos) resources.getColor(
                    R.color.res_color_ff824c
                ) else resources.getColor(R.color.res_color_font_normal)
            )
            // 项目
            it.vProject.visibility = if (pos == 2) View.VISIBLE else View.INVISIBLE
            it.tvProject.setTextColor(
                if (2 == pos) resources.getColor(
                    R.color.res_color_ff824c
                ) else resources.getColor(R.color.res_color_font_normal)
            )
            // 导航
            it.vNav.visibility = if (pos == 3) View.VISIBLE else View.INVISIBLE
            it.tvNav.setTextColor(
                if (3 == pos) resources.getColor(
                    R.color.res_color_ff824c
                ) else resources.getColor(R.color.res_color_font_normal)
            )
        }
    }

}